package com.cmcc.xdr.util;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import com.cmcc.xdr.message.SingleSigBiccMsg;
import com.cmcc.xdr.message.SingleSigGlobalMsg;
import com.cmcc.xdr.message.SingleSigMsg;
import com.cmcc.xdr.message.SingleSigSrvccMsg;
import com.cmcc.xdr.message.SynComposedMsg;
import com.cmcc.xdr.util.Interface;
import com.cmcc.xdr.util.XdrFactory;

public class XdrSingleSigMsgParseUtil {
	public static List<SingleSigMsg> parse(ByteBuffer buffer,String divisionXdrType,String xdrVersion)
	{
		SingleSigGlobalMsg globalMsg = new SingleSigGlobalMsg(buffer,divisionXdrType);
		String tableName = "";
		List<SingleSigMsg> list = new ArrayList<SingleSigMsg>();
		
		/////////////////////////////////////////////////////////////////////////////////
		if(globalMsg.getXdrInfoType() == 2)
		{//单接口信息
			
			if(globalMsg.getInterfaceType()==29||globalMsg.getInterfaceType()==13||
			   globalMsg.getInterfaceType()==14||globalMsg.getInterfaceType()==15||
			   globalMsg.getInterfaceType()==16||globalMsg.getInterfaceType()==17||
			   globalMsg.getInterfaceType()==18||globalMsg.getInterfaceType()==27)
			{
				tableName = "xdr_sipcall";
				SingleSigMsg sss = (SingleSigMsg)XdrFactory.getInstance(tableName, buffer, globalMsg, xdrVersion);
				list.add(sss);
			}
			
			if(globalMsg.getInterfaceType()==19)
			{
				tableName = "xdr_srvcc";
				SingleSigSrvccMsg sv = (SingleSigSrvccMsg)XdrFactory.getInstance(tableName, buffer, globalMsg, xdrVersion);
				list.add(sv);
			}
			
			if(globalMsg.getInterfaceType()==24||globalMsg.getInterfaceType()==20||
			   globalMsg.getInterfaceType()==21||globalMsg.getInterfaceType()==22||
			   globalMsg.getInterfaceType()==23)
			{
				tableName = "xdr_authen";
				SingleSigMsg authen = (SingleSigMsg)XdrFactory.getInstance(tableName, buffer, globalMsg, xdrVersion);
				list.add(authen);
			}
			
			if(globalMsg.getInterfaceType()==25)
			{
				tableName = "xdr_pcc";
				SingleSigMsg pcc_25 = (SingleSigMsg)XdrFactory.getInstance(tableName, buffer, globalMsg, xdrVersion);
				list.add(pcc_25);
			}
			
			if(globalMsg.getInterfaceType()==26)
			{
				tableName = "xdr_pcc";
				SingleSigMsg pcc = (SingleSigMsg)XdrFactory.getInstance(tableName, buffer, globalMsg, xdrVersion);
				list.add(pcc);
			}
			
			if(globalMsg.getInterfaceType()==28)
			{
				tableName = "xdr_bicc";
				SingleSigBiccMsg nc= new SingleSigBiccMsg(buffer,globalMsg);
				nc.getSSGMsg().setInterfaceType(nc.getNc().getInterFace());
				list.add(nc);
			}
		}
		else 
		if(globalMsg.getXdrInfoType() == 1)
		{
			/**
			 * =================================================
			 * =========合成接口 Message Body 信息处理==开始============
			 * =================================================
			 */
			tableName = "xdr_composed";
//			List<SynComposedMsg> composedMsgs = new ArrayList<SynComposedMsg>();
			for(int s = 0;s < globalMsg.getXdrNumber();s++){
				SynComposedMsg composed = new SynComposedMsg(buffer,globalMsg);
				composed.getSSGMsg().setInterfaceType(-1);//合成接口
				list.add(composed);
//				manage.addToList(composed);
			}
			//解析业务场景字段
//			String scenario = getServiceScenario(composedMsgs);
//			for (SynComposedMsg synComposedMsg : composedMsgs) { 
//				synComposedMsg.composed.setServiceScenario(scenario);
//				/*else{
//					manageForComposedReg.addToList(synComposedMsg);
//				}*/
//			}
			//重新构造一条单独存公共信息和通用信息的记录
			SynComposedMsg fartherComposedMsg = new SynComposedMsg(globalMsg);
//			fartherComposedMsg.composed.setServiceScenario(scenario);
			fartherComposedMsg.getSSGMsg().setInterfaceType(-1);//合成接口
			list.add(fartherComposedMsg);
		}
		else
		{
			//logger.error("--globalMsg---message-----"+DatatypeConverter.printHexBinary(message.getBuffer().array()));
		}
		/////////////////////////////////////////////////////////////////////////////////
		return list;
	}
}
